package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.entity.mongo.SundayCenterMessage;
import org.itboys.core.service.mongo.web.SundayCenterMessageService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/14
 * Description: 消息中心控制器
 */

@Controller
@RequestMapping("/sunday/web/centerMessage")
public class SundayCenterMessageController extends BaseController{
    @Autowired
    private SundayCenterMessageService centerMessageService;

    /**
     * 消息中心首页
     * @return
     */
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                         Model model){
        return "/sunday/centerMessage/index";
    }

    /**
     * 分页查询
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request
            , HttpServletResponse response){
        try {
            PageResult<SundayCenterMessage> result = centerMessageService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 更新或者保存
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value = "id") Long id,
                     @RequestParam(value = "memberId") Long memberId,
                     @RequestParam(value = "memberName") String memberName,
                     @RequestParam(value = "name") String name,
                     @RequestParam(value = "text") String text

    ) {
        try {
            SundayCenterMessage message = new SundayCenterMessage(memberId, memberName, name, text);

            centerMessageService.save(id,message);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = " ";
            if (e.getClass().getSimpleName().equalsIgnoreCase("Error Exception")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/centerMessage/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }

    @RequestMapping("/setStatus")
    public void setStatus(HttpServletRequest request,
                          HttpServletResponse response,
                          @RequestParam(value = "id", required = true) Long id,
                          @RequestParam(value = "status", required = true) Integer status
    ) {
        try {
            centerMessageService.setStatus(id, status);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/centerMessage/setStatus------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    @RequestMapping("/findOne")
    public void findOne(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value = "id", required = true) Long id
    ) {
        try {

            SundayCenterMessage message = centerMessageService.findOne(id);
            AjaxUtils.renderJson(response, message);
        } catch (Exception e) {
            e.printStackTrace();

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/centerMessage/findOne------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

}
